﻿var Hint = new (function () {

    //Start of all of the Private members & methods

    var
	This = this,
	root = $('<div>'),
	dom = root[0],
	text = document.createTextNode(' '),
	hasLoaded = false,
	hasFrameCall = false,
	status = 0,
		//0 - inactive
		//1 - active
	init = function () {

	    dom.style.position = 'absolute';
	    dom.style.left = '0px';
	    dom.style.top = '0px';
	    dom.style.whiteSpace = 'nowrap';

	    dom.className = 'HintObject';

	    dom.style.zIndex = 100000;

	    dom.appendChild(text);
	    root.hide();
	},
	frameCall = function () {

	    if (!status) {
	        hasFrameCall = false;
	        return;
	    }

	    var margin = 20, offset,
			width = parseFloat(dom.offsetWidth),
			height = parseFloat(dom.offsetHeight);

	    if (width) {
	        var windowPos = Utility.getWindowDimentions();

	        offset = (windowPos.width + windowPos.scrollLeft) - Utility.Mouse.x - width - 25 - margin;
	        dom.style.left = (Utility.Mouse.x + margin + (offset < 0 ? offset : 0)) + 'px';

	        offset = (windowPos.height + windowPos.scrollTop) - Utility.Mouse.y - height - 25 - margin;
	        dom.style.top = (offset > 0 ? (Utility.Mouse.y + margin) : (Utility.Mouse.y - (margin / 2) - height)) + 'px';
	    }

	    hasFrameCall = true;
	};

    //End of all of the Private members | Start of all of the public members

    this.load = function () {
        if (hasLoaded)
            return;
        hasLoaded = true;
        document.body.appendChild(dom);
        Utility.addFrameCall(frameCall);
    };
    this.show = function (strMessage) {

        if (!hasLoaded)
            This.load();

        dom.removeChild(text);
        text = document.createTextNode(strMessage)
        dom.appendChild(text);
        status = 1;

        root.stop(true).fadeTo(null, 1);

        frameCall();
    };
    this.hide = function () {
        root.fadeOut(null, function () { This.status = 0; });
    };

    init();
})();